####################################################
## Replication: "Technology and Collective Action ##
####################################################

# Due to data-sharing limitations by the GSMA, this archive only contains a reduced
# dataset without any information on cell phone coverage. 


rm(list=ls(all=TRUE))

library(foreign)
library(xtable)
library(Zelig)
library(countrycode)
library(AER)
library(ggplot2)
library(memisc)
library(survival)

### Load data

data_all <- read.dta("ReplicationCellPhones.dta")


# gid: grid ID
# cow: COW country code
# bdist1: border Distance
# capdist: capital distance
# pop2005: population count 2005
# pre2000_count: pre-2000 UCDP conflict count
# conf2008_dum: conflict dummy, 2008
# conf2008_count: conflict count, 2008
# mnt: mountainous terrain
# irri: % irrigatated
# gcppc05: GDP per capita in 2005
# splag07_count: spatial lag for UCDP count
# splag07_dum: spatial lag for UCDP binary
# reg: average World Bank regulatory score
# diamond: dummy for diamond deposit
# oil: dummy for oil and gas deposit
# excluded: share of local groups that are nationally excluded
# conf2008_dum2: conflict dummy UCDP, 2008, precise
# conf2008_count2: conflict count UCDP, 2008, precise
# pre2000_count2: pre-2000 UCDP conflict count, precise
# splag07_dum2: spatial lag for UCDP binary, precise
# splag07_count2: spatial lag for UCDP count, precise
# conf2008_dumAC: ACLED binary conflict, 2008
# conf2008_countAC: ACLED conflict count, 2008
# pre2000_countAC: ACLED pre2000 conflict count
# splag07_dumAC: spatial lag conflict binary ACLED
# splag07_countAC: spatial lag conflict count ACLED
# conf2008_dumScad: SCAD binary conflict 2008
# conf2008_countScad: SCAD conflict count, 2008
# splag07_dumScad: spatial lag SCAD binary conflict
# splag07_countScad: spatial lag SCAD conflict count


#####################################
#### FINAL MODELS, results in paper are w/o logging gdp pc
#####################################
#standard logit
m1 <- zelig(conf2008_dum~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="logit",data=data_all,robust=TRUE)
summary(m1)

#re-logit
m2 <- zelig(conf2008_dum~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="relogit",robust=TRUE,data=data_all)
summary(m2)

#mixed effects logit
m3 <- zelig(conf2008_dum~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07+tag(1|cow),model="logit.mixed",data=data_all)
summary(m3)

#mixed effects logit with country mean
m4 <- zelig(conf2008_dum~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07+cmean_07+tag(1|cow),model="logit.mixed",data=data_all)
summary(m4)

#OLS with fixed effects
m5 <- zelig(conf2008_dum~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07+factor(cow),model="ls",data=data_all,robust=TRUE)
summary(m5)

#standard logit w/o cell phone
m11 <- zelig(conf2008_dum~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00,model="logit",data=data_all,robust=TRUE)
summary(m11)

#Table, relogit is not recognized, have to substitute real values
toLatex(
  relabel(
    mtable("Logit, robust SE"=m11,"Logit, robust SE"=m1, "Re-Logit, robust SE"=m1,"Mixed Effects Logit"=m3,"Mixed Effects Logit"=m4,"Fixed Effects OLS, robust SE"=m5,coef.style="stat",
           summary.stats=c("AIC","BIC","Deviance","Log-likelihood","N")),
    pre2000_count="pre-2000 Conflict",
    bdist1="Border Distance",
    capdist="Capital Distance",
    pop2005="Population",
    mnt="Pct Mountainous ",
    irri="Pct Irrigation",
    gcppc00="GDP pc",
    cell_dum_07="Cellphone Coverage",
    cmean_07="Mean Cell Coverage",
    gsub=TRUE),ddigits=7)

lrtest(m1,m11)

m6 <- zelig(conf2008_count~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="poisson",data=data_all,robust=TRUE)
summary(m6)

m9 <- zelig(conf2008_count~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="negbin",data=data_all,robust=TRUE)
summary(m9)

m6b <- zelig(conf2008_count~splag07_count+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="poisson",data=data_all,robust=TRUE)
summary(m6b)

m9b <- zelig(conf2008_count~splag07_count+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="negbin",data=data_all,robust=TRUE)
summary(m9b)


#Table
toLatex(
  relabel(
    mtable("Poisson, robust SE"=m6, "Negative Binomial, robust SE"=m9,"Poisson, robust SE"=m6b,"Negative Binomial, robust SE"=m9b,coef.style="stat",
           summary.stats=c("AIC","BIC","Deviance","Log-likelihood","N")),
    pre2000_count="pre-2000 Conflict",
    bdist1="Border Distance",
    splag07_count="Spatial Lag",
    capdist="Capital Distance",
    pop2005="Population",
    mnt="Pct Mountainous ",
    irri="Pct Irrigation",
    gcppc00="GDP pc",
    cell_dum_07="Cellphone Coverage",
    cmean_07="Mean Cell Coverage",
    gsub=TRUE),ddigits=5)


m10 <- zelig(conf2008_dum~splag07_dum+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="logit",data=data_all,robust=TRUE)
summary(m10)

m11 <- zelig(conf2008_dum~splag07_dum+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="relogit",data=data_all,robust=TRUE)
summary(m11)

m12 <- zelig(conf2008_dum~splag07_dum+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07+tag(1|cow),model="logit.mixed",data=data_all)
summary(m12)

m13 <- zelig(conf2008_dum~splag07_dum+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07+cmean_07+tag(1|cow),model="logit.mixed",data=data_all,robust=TRUE)
summary(m13)

m14 <- zelig(conf2008_dum~splag07_dum+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07+factor(cow),model="ls",data=data_all,robust=TRUE)
summary(m14)

#Table, relogit is not recognized, have to substitute real values
toLatex(
  relabel(
    mtable("Logit, robust SE"=m10,"Re-Logit, robust SE"=m10, "Mixed Logit"=m12,"Mixed Logit"=m13,"OLS FE, robust SE"=m14,coef.style="stat",
           summary.stats=c("AIC","BIC","Deviance","Log-likelihood","N")),
    splag07_dum="Spatial Lag",
    splag07_count="Spatial Lag",
    pre2000_count="pre-2000 Conflict",
    bdist1="Border Distance",
    capdist="Capital Distance",
    pop2005="Population",
    mnt="Pct Mountainous ",
    irri="Pct Irrigation",
    gcppc00="GDP pc",
    cell_dum_07="Cellphone Coverage",
    cmean_07="Mean Cell Coverage",
    gsub=TRUE),ddigits=5)


#IV
m15 <- ivreg(conf2008_dum~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07|pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+reg,data=data_all)
summary(m15)

m16 <- ivreg(conf2008_dum~splag07_dum+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07|splag07_dum+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+reg,data=data_all)
summary(m16)

#Table
toLatex(
  relabel(
    mtable("2SLS"=m15,"2SLS"=m16,coef.style="stat",
           summary.stats=c("AIC","BIC","Deviance","Log-likelihood","N")),
    splag07_dum="Spatial Lag",
    splag07_count="Spatial Lag",
    pre2000_count="pre-2000 Conflict",
    bdist1="Border Distance",
    capdist="Capital Distance",
    pop2005="Population",
    mnt="Pct Mountainous ",
    irri="Pct Irrigation",
    gcppc00="GDP pc",
    cell_dum_07="Cellphone Coverage",
    cmean_07="Mean Cell Coverage",
    gsub=TRUE),ddigits=5)

######################################
##### SUBSTANTIVE EFFECTS
######################################


#Binary DV effect 
BDV <- matrix(,nrow=5,ncol=4)

x0 <- setx(m1,cell_dum_07=0)  
x1 <- setx(m1,cell_dum_07=1)
s.out <- sim(m1,x=x0,x1=x1)
BDV[1,] <- summary(s.out)$qi.stats$fd

x0 <- setx(m2,cell_dum_07=0)  
x1 <- setx(m2,cell_dum_07=1)
s.out <- sim(m2,x=x0,x1=x1)
BDV[2,] <- summary(s.out)$qi.stats$fd

x0 <- setx(m3,cell_dum_07=0)  
x1 <- setx(m3,cell_dum_07=1)
s.out <- sim(m3,x=x0,x1=x1)
BDV[3,] <- summary(s.out)$qi.stats$fd

x0 <- setx(m4,cell_dum_07=0)  
x1 <- setx(m4,cell_dum_07=1)
s.out <- sim(m4,x=x0,x1=x1)
BDV[4,] <- summary(s.out)$qi.stats$fd


x0 <- setx(m5,cell_dum_07=0,cow2=615)  
x1 <- setx(m5,cell_dum_07=1,cow2=615)
s.out <- sim(m5,x=x0,x1=x1)
BDV[5,] <- summary(s.out)$qi.stats$fd

rownames(BDV) <- c("1) Logit","2) Re-Logit","3) Mixed Logit","4) Mixed Logit, Country Means",
                   "5) OLS FE")





#Binary DV plot
alpha <- 0.05
modelnames<-c("Binary DV")

Multiplier <- qnorm(1 - alpha / 2)
CoefficientTables <- list(BDV)
TableRows <- unlist(lapply(CoefficientTables, nrow))

if(modelnames[1] == ""){
  ModelNameLabels <- rep(paste("Model", 1:length(TableRows)), TableRows)
} else {
  ModelNameLabels <- rep(modelnames, TableRows)
}

MatrixofModels <- cbind(do.call(rbind, CoefficientTables), ModelNameLabels)
MatrixofModels <- data.frame(cbind(rownames(MatrixofModels), MatrixofModels))
colnames(MatrixofModels) <- c("IV", "Estimate", "StandardError", "low", "high", "ModelName")
MatrixofModels$IV <- factor(MatrixofModels$IV, levels = rev(MatrixofModels$IV))
MatrixofModels[, -c(1, 6)] <- apply(MatrixofModels[, -c(1, 6)], 2, function(x){as.numeric(as.character(x))})

OutputPlot <- qplot(IV, Estimate, ymin = low,
                    ymax = high, data = MatrixofModels, geom = "pointrange",
                    ylab = NULL, xlab = NULL)
OutputPlot <- OutputPlot + geom_hline(yintercept = 0, lwd = I(7/12), colour ="gray", alpha = I(5/12))
OutputPlot <- OutputPlot + facet_grid(~ ModelName) + coord_flip() + theme_bw()
OutputPlot


OutputPlot <- OutputPlot + geom_hline(yintercept = 0, lwd = I(7/12), colour ="gray", alpha = I(5/12))





#####################################
#### Robustness check  oil, diamonds
#####################################
#standard logit
m1 <- zelig(conf2008_dum~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+diamond+oil+cell_dum_07,model="logit",data=data_all,robust=TRUE)
summary(m1)

#re-logit
m2 <- zelig(conf2008_dum~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+diamond+oil+cell_dum_07,model="relogit",robust=TRUE,data=data_all)
summary(m2)

#mixed effects logit
m3 <- zelig(conf2008_dum~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+diamond+oil+cell_dum_07+tag(1|cow),model="logit.mixed",data=data_all)
summary(m3)

#mixed effects logit with country mean
m4 <- zelig(conf2008_dum~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+diamond+oil+cell_dum_07+cmean_07+tag(1|cow),model="logit.mixed",data=data_all)
summary(m4)

#OLS with fixed effects
m5 <- zelig(conf2008_dum~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+diamond+oil+cell_dum_07+factor(cow),model="ls",data=data_all,robust=TRUE)
summary(m5)

#Table, relogit is not recognized, have to substitute real values
toLatex(
  relabel(
    mtable("Logit, robust SE"=m1, "Re-Logit, robust SE"=m1,"Mixed Effects Logit"=m3,"Mixed Effects Logit"=m4,"Fixed Effects OLS, robust SE"=m5,coef.style="stat",
           summary.stats=c("AIC","BIC","Deviance","Log-likelihood","N")),
    pre2000_count="pre-2000 Conflict",
    bdist1="Border Distance",
    capdist="Capital Distance",
    pop2005="Population",
    mnt="Pct Mountainous ",
    irri="Pct Irrigation",
    gcppc00="GDP pc",
    cell_dum_07="Cellphone Coverage",
    cmean_07="Mean Cell Coverage",
    gsub=TRUE),ddigits=7)



m6 <- zelig(conf2008_count~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+diamond+oil+cell_dum_07,model="poisson",data=data_all,robust=TRUE)
summary(m6)

m7 <- zelig(conf2008_count~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+diamond+oil+cell_dum_07+tag(1|cow),model="poisson.mixed",data=data_all,robust=TRUE)
summary(m7)

m8 <- zelig(conf2008_count~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+diamond+oil+cell_dum_07+cmean_07+tag(1|cow),model="poisson.mixed",data=data_all,robust=TRUE)
summary(m8)

m9 <- zelig(conf2008_count~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+diamond+oil+cell_dum_07,model="negbin",data=data_all,robust=TRUE)
summary(m9)

#Table
toLatex(
  relabel(
    mtable("Poisson, robust SE"=m6, "Negative Binomial, robust SE"=m9,coef.style="stat",
           summary.stats=c("AIC","BIC","Deviance","Log-likelihood","N")),
    pre2000_count="pre-2000 Conflict",
    bdist1="Border Distance",
    capdist="Capital Distance",
    pop2005="Population",
    mnt="Pct Mountainous ",
    irri="Pct Irrigation",
    gcppc00="GDP pc",
    cell_dum_07="Cellphone Coverage",
    cmean_07="Mean Cell Coverage",
    gsub=TRUE),ddigits=5)


m10 <- zelig(conf2008_dum~splag07_dum+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+diamond+oil+cell_dum_07,model="logit",data=data_all,robust=TRUE)
summary(m10)

m11 <- zelig(conf2008_dum~splag07_dum+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+diamond+oil+cell_dum_07,model="relogit",data=data_all,robust=TRUE)
summary(m11)

m12 <- zelig(conf2008_dum~splag07_dum+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+diamond+oil+cell_dum_07+cmean_07+tag(1|cow),model="logit.mixed",data=data_all)
summary(m12)

m13 <- zelig(conf2008_count~splag07_count+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+diamond+oil+cell_dum_07,model="poisson",data=data_all,robust=TRUE)
summary(m13)

m14 <- zelig(conf2008_count~splag07_count+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+diamond+oil+cell_dum_07,model="negbin",data=data_all,robust=TRUE)
summary(m14)


#Table, relogit is not recognized, have to substitute real values
toLatex(
  relabel(
    mtable("Logit, robust SE"=m10,"Re-Logit, robust SE"=m10, "Mixed Logit"=m12,"Poisson, robust SE"=m13,"Negative Binomial, robust SE"=m14,coef.style="stat",
           summary.stats=c("AIC","BIC","Deviance","Log-likelihood","N")),
    splag07_dum="Spatial Lag",
    splag07_count="Spatial Lag",
    pre2000_count="pre-2000 Conflict",
    bdist1="Border Distance",
    capdist="Capital Distance",
    pop2005="Population",
    mnt="Pct Mountainous ",
    irri="Pct Irrigation",
    gcppc00="GDP pc",
    cell_dum_07="Cellphone Coverage",
    cmean_07="Mean Cell Coverage",
    gsub=TRUE),ddigits=5)


#IV
m15 <- ivreg(conf2008_dum~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+diamond+oil+cell_dum_07|pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+diamond+oil+reg,data=data_all)
summary(m15)

m16 <- ivreg(conf2008_dum~splag07_dum+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+diamond+oil+cell_dum_07|splag07_dum+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+diamond+oil+reg,data=data_all)
summary(m16)

#Table
toLatex(
  relabel(
    mtable("2SLS"=m15,"2SLS"=m16,coef.style="stat",
           summary.stats=c("AIC","BIC","Deviance","Log-likelihood","N")),
    splag07_dum="Spatial Lag",
    splag07_count="Spatial Lag",
    pre2000_count="pre-2000 Conflict",
    bdist1="Border Distance",
    capdist="Capital Distance",
    pop2005="Population",
    mnt="Pct Mountainous ",
    irri="Pct Irrigation",
    gcppc00="GDP pc",
    cell_dum_07="Cellphone Coverage",
    cmean_07="Mean Cell Coverage",
    gsub=TRUE),ddigits=5)


#####################################
#### Robustness Ethnicity
#####################################
#standard logit
m1 <- zelig(conf2008_dum~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+excluded+cell_dum_07,model="logit",data=data_all,robust=TRUE)
summary(m1)

#re-logit
m2 <- zelig(conf2008_dum~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+excluded+cell_dum_07,model="relogit",robust=TRUE,data=data_all)
summary(m2)

#mixed effects logit
m3 <- zelig(conf2008_dum~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+excluded+cell_dum_07+tag(1|cow),model="logit.mixed",data=data_all)
summary(m3)

#mixed effects logit with country mean
m4 <- zelig(conf2008_dum~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+excluded+cell_dum_07+cmean_07+tag(1|cow),model="logit.mixed",data=data_all)
summary(m4)

#OLS with fixed effects
m5 <- zelig(conf2008_dum~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+excluded+cell_dum_07+factor(cow),model="ls",data=data_all,robust=TRUE)
summary(m5)

#Table, relogit is not recognized, have to substitute real values
toLatex(
  relabel(
    mtable("Logit, robust SE"=m1, "Re-Logit, robust SE"=m1,"Mixed Effects Logit"=m3,"Mixed Effects Logit"=m4,"Fixed Effects OLS, robust SE"=m5,coef.style="stat",
           summary.stats=c("AIC","BIC","Deviance","Log-likelihood","N")),
    pre2000_count="pre-2000 Conflict",
    bdist1="Border Distance",
    capdist="Capital Distance",
    pop2005="Population",
    mnt="Pct Mountainous ",
    irri="Pct Irrigation",
    gcppc00="GDP pc",
    cell_dum_07="Cellphone Coverage",
    cmean_07="Mean Cell Coverage",
    gsub=TRUE),ddigits=7)

m6 <- zelig(conf2008_count~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+excluded+cell_dum_07,model="poisson",data=data_all,robust=TRUE)
summary(m6)

m7 <- zelig(conf2008_count~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+excluded+cell_dum_07+tag(1|cow),model="poisson.mixed",data=data_all,robust=TRUE)
summary(m7)

m8 <- zelig(conf2008_count~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+excluded+cell_dum_07+cmean_07+tag(1|cow),model="poisson.mixed",data=data_all,robust=TRUE)
summary(m8)

m9 <- zelig(conf2008_count~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+excluded+cell_dum_07,model="negbin",data=data_all,robust=TRUE)
summary(m9)

#Table
toLatex(
  relabel(
    mtable("Poisson, robust SE"=m6, "Negative Binomial, robust SE"=m9,coef.style="stat",
           summary.stats=c("AIC","BIC","Deviance","Log-likelihood","N")),
    pre2000_count="pre-2000 Conflict",
    bdist1="Border Distance",
    capdist="Capital Distance",
    pop2005="Population",
    mnt="Pct Mountainous ",
    irri="Pct Irrigation",
    gcppc00="GDP pc",
    cell_dum_07="Cellphone Coverage",
    cmean_07="Mean Cell Coverage",
    gsub=TRUE),ddigits=5)


m10 <- zelig(conf2008_dum~splag07_dum+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+excluded+cell_dum_07,model="logit",data=data_all,robust=TRUE)
summary(m10)

m11 <- zelig(conf2008_dum~splag07_dum+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+excluded+cell_dum_07,model="relogit",data=data_all,robust=TRUE)
summary(m11)

m12 <- zelig(conf2008_dum~splag07_dum+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+excluded+cell_dum_07+cmean_07+tag(1|cow),model="logit.mixed",data=data_all)
summary(m12)

m13 <- zelig(conf2008_count~splag07_count+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+excluded+cell_dum_07,model="poisson",data=data_all,robust=TRUE)
summary(m13)

m14 <- zelig(conf2008_count~splag07_count+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+excluded+cell_dum_07,model="negbin",data=data_all,robust=TRUE)
summary(m14)

#Table, relogit is not recognized, have to substitute real values
toLatex(
  relabel(
    mtable("Logit, robust SE"=m10,"Re-Logit, robust SE"=m10, "Mixed Logit"=m12,"Poisson, robust SE"=m13,"Negative Binomial, robust SE"=m14,coef.style="stat",
           summary.stats=c("AIC","BIC","Deviance","Log-likelihood","N")),
    splag07_dum="Spatial Lag",
    splag07_count="Spatial Lag",
    pre2000_count="pre-2000 Conflict",
    bdist1="Border Distance",
    capdist="Capital Distance",
    pop2005="Population",
    mnt="Pct Mountainous ",
    irri="Pct Irrigation",
    gcppc00="GDP pc",
    cell_dum_07="Cellphone Coverage",
    cmean_07="Mean Cell Coverage",
    gsub=TRUE),ddigits=5)


#IV
m15 <- ivreg(conf2008_dum~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+excluded+cell_dum_07|pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+excluded+reg,data=data_all)
summary(m15)

m16 <- ivreg(conf2008_dum~splag07_dum+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+excluded+cell_dum_07|splag07_dum+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+excluded+reg,data=data_all)
summary(m16)

#Table
toLatex(
  relabel(
    mtable("2SLS"=m15,"2SLS"=m16,coef.style="stat",
           summary.stats=c("AIC","BIC","Deviance","Log-likelihood","N")),
    splag07_dum="Spatial Lag",
    splag07_count="Spatial Lag",
    pre2000_count="pre-2000 Conflict",
    bdist1="Border Distance",
    capdist="Capital Distance",
    pop2005="Population",
    mnt="Pct Mountainous ",
    irri="Pct Irrigation",
    gcppc00="GDP pc",
    cell_dum_07="Cellphone Coverage",
    cmean_07="Mean Cell Coverage",
    gsub=TRUE),ddigits=5)



#####################################
#### ROBUSTNESS UCDP1-3
#####################################
#standard logit
m1 <- zelig(conf2008_dum2~pre2000_count2+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="logit",data=data_all,robust=TRUE)
summary(m1)

#re-logit
m2 <- zelig(conf2008_dum2~pre2000_count2+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="relogit",robust=TRUE,data=data_all)
summary(m2)

#mixed effects logit
m3 <- zelig(conf2008_dum2~pre2000_count2+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07+tag(1|cow),model="logit.mixed",data=data_all)
summary(m3)

#mixed effects logit with country mean
m4 <- zelig(conf2008_dum2~pre2000_count2+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07+cmean_07+tag(1|cow),model="logit.mixed",data=data_all)
summary(m4)

#OLS with fixed effects
m5 <- zelig(conf2008_dum2~pre2000_count2+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07+factor(cow),model="ls",data=data_all,robust=TRUE)
summary(m5)

#Table, relogit is not recognized, have to substitute real values
toLatex(
  relabel(
    mtable("Logit, robust SE"=m1, "Re-Logit, robust SE"=m1,"Mixed Effects Logit"=m3,"Mixed Effects Logit"=m4,"Fixed Effects OLS, robust SE"=m5,coef.style="stat",
           summary.stats=c("AIC","BIC","Deviance","Log-likelihood","N")),
    pre2000_count="pre-2000 Conflict",
    bdist1="Border Distance",
    capdist="Capital Distance",
    pop2005="Population",
    mnt="Pct Mountainous ",
    irri="Pct Irrigation",
    gcppc00="GDP pc",
    cell_dum_07="Cellphone Coverage",
    cmean_07="Mean Cell Coverage",
    gsub=TRUE),ddigits=7)



m6 <- zelig(conf2008_count2~pre2000_count2+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="poisson",data=data_all,robust=TRUE)
summary(m6)

m7 <- zelig(conf2008_count2~pre2000_count2+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07+tag(1|cow),model="poisson.mixed",data=data_all,robust=TRUE)
summary(m7)

m8 <- zelig(conf2008_count2~pre2000_count2+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07+cmean_07+tag(1|cow),model="poisson.mixed",data=data_all,robust=TRUE)
summary(m8)

m9 <- zelig(conf2008_count2~pre2000_count2+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="negbin",data=data_all,robust=TRUE)
summary(m9)

#Table
toLatex(
  relabel(
    mtable("Poisson, robust SE"=m6, "Negative Binomial, robust SE"=m9,coef.style="stat",
           summary.stats=c("AIC","BIC","Deviance","Log-likelihood","N")),
    pre2000_count="pre-2000 Conflict",
    bdist1="Border Distance",
    capdist="Capital Distance",
    pop2005="Population",
    mnt="Pct Mountainous ",
    irri="Pct Irrigation",
    gcppc00="GDP pc",
    cell_dum_07="Cellphone Coverage",
    cmean_07="Mean Cell Coverage",
    gsub=TRUE),ddigits=5)


m10 <- zelig(conf2008_dum2~splag07_dum2+pre2000_count2+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="logit",data=data_all,robust=TRUE)
summary(m10)

m11 <- zelig(conf2008_dum2~splag07_dum2+pre2000_count2+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="relogit",data=data_all,robust=TRUE)
summary(m11)

m12 <- zelig(conf2008_dum2~splag07_dum2+pre2000_count2+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07+cmean_07+tag(1|cow),model="logit.mixed",data=data_all)
summary(m12)

m13 <- zelig(conf2008_count2~splag07_count2+pre2000_count2+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="poisson",data=data_all,robust=TRUE)
summary(m13)

m14 <- zelig(conf2008_count2~splag07_count2+pre2000_count2+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="negbin",data=data_all,robust=TRUE)
summary(m14)

#Table, relogit is not recognized, have to substitute real values
toLatex(
  relabel(
    mtable("Logit, robust SE"=m10,"Re-Logit, robust SE"=m10, "Mixed Logit"=m12,"Poisson, robust SE"=m13,"Negative Binomial, robust SE"=m14,coef.style="stat",
           summary.stats=c("AIC","BIC","Deviance","Log-likelihood","N")),
    splag07_dum="Spatial Lag",
    splag07_count="Spatial Lag",
    pre2000_count="pre-2000 Conflict",
    bdist1="Border Distance",
    capdist="Capital Distance",
    pop2005="Population",
    mnt="Pct Mountainous ",
    irri="Pct Irrigation",
    gcppc00="GDP pc",
    cell_dum_07="Cellphone Coverage",
    cmean_07="Mean Cell Coverage",
    gsub=TRUE),ddigits=5)


#IV
m15 <- ivreg(conf2008_dum2~pre2000_count2+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07|pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+reg,data=data_all)
summary(m15)

m16 <- ivreg(conf2008_dum2~splag07_dum2+pre2000_count2+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07|splag07_dum+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+reg,data=data_all)
summary(m16)

#Table
toLatex(
  relabel(
    mtable("2SLS"=m15,"2SLS"=m16,coef.style="stat",
           summary.stats=c("AIC","BIC","Deviance","Log-likelihood","N")),
    splag07_dum="Spatial Lag",
    splag07_count="Spatial Lag",
    pre2000_count="pre-2000 Conflict",
    bdist1="Border Distance",
    capdist="Capital Distance",
    pop2005="Population",
    mnt="Pct Mountainous ",
    irri="Pct Irrigation",
    gcppc00="GDP pc",
    cell_dum_07="Cellphone Coverage",
    cmean_07="Mean Cell Coverage",
    gsub=TRUE),ddigits=5)



#####################################
#### ROBUSTNESS ACLED
#####################################
#standard logit
m1 <- zelig(conf2008_dumAC~pre2000_countAC+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="logit",data=data_all,robust=TRUE)
summary(m1)

#re-logit
m2 <- zelig(conf2008_dumAC~pre2000_countAC+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="relogit",robust=TRUE,data=data_all)
summary(m2)

#mixed effects logit
m3 <- zelig(conf2008_dumAC~pre2000_countAC+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07+tag(1|cow),model="logit.mixed",data=data_all)
summary(m3)

#mixed effects logit with country mean
m4 <- zelig(conf2008_dumAC~pre2000_countAC+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07+cmean_07+tag(1|cow),model="logit.mixed",data=data_all)
summary(m4)

#OLS with fixed effects
m5 <- zelig(conf2008_dumAC~pre2000_countAC+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07+factor(cow),model="ls",data=data_all,robust=TRUE)
summary(m5)

#Table, relogit is not recognized, have to substitute real values
toLatex(
  relabel(
    mtable("Logit, robust SE"=m1, "Re-Logit, robust SE"=m1,"Mixed Effects Logit"=m3,"Mixed Effects Logit"=m4,"Fixed Effects OLS, robust SE"=m5,coef.style="stat",
           summary.stats=c("AIC","BIC","Deviance","Log-likelihood","N")),
    pre2000_count="pre-2000 Conflict",
    bdist1="Border Distance",
    capdist="Capital Distance",
    pop2005="Population",
    mnt="Pct Mountainous ",
    irri="Pct Irrigation",
    gcppc00="GDP pc",
    cell_dum_07="Cellphone Coverage",
    cmean_07="Mean Cell Coverage",
    gsub=TRUE),ddigits=7)



m6 <- zelig(conf2008_countAC~pre2000_countAC+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="poisson",data=data_all,robust=TRUE)
summary(m6)

m7 <- zelig(conf2008_countAC~pre2000_countAC+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07+tag(1|cow),model="poisson.mixed",data=data_all,robust=TRUE)
summary(m7)

m8 <- zelig(conf2008_countAC~pre2000_countAC+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07+cmean_07+tag(1|cow),model="poisson.mixed",data=data_all,robust=TRUE)
summary(m8)

m9 <- zelig(conf2008_countAC~pre2000_countAC+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="negbin",data=data_all,robust=TRUE)
summary(m9)


#Table
toLatex(
  relabel(
    mtable("Poisson, robust SE"=m6, "Negative Binomial, robust SE"=m9,coef.style="stat",
           summary.stats=c("AIC","BIC","Deviance","Log-likelihood","N")),
    pre2000_count="pre-2000 Conflict",
    bdist1="Border Distance",
    capdist="Capital Distance",
    pop2005="Population",
    mnt="Pct Mountainous ",
    irri="Pct Irrigation",
    gcppc00="GDP pc",
    cell_dum_07="Cellphone Coverage",
    cmean_07="Mean Cell Coverage",
    gsub=TRUE),ddigits=5)


m10 <- zelig(conf2008_dumAC~splag07_dumAC+pre2000_countAC+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="logit",data=data_all,robust=TRUE)
summary(m10)

m11 <- zelig(conf2008_dumAC~splag07_dumAC+pre2000_countAC+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="relogit",data=data_all,robust=TRUE)
summary(m11)

m12 <- zelig(conf2008_dumAC~splag07_dumAC+pre2000_countAC+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07+cmean_07+tag(1|cow),model="logit.mixed",data=data_all)
summary(m12)

m13 <- zelig(conf2008_countAC~splag07_countAC+pre2000_countAC+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="poisson",data=data_all,robust=TRUE)
summary(m13)

m14 <- zelig(conf2008_countAC~splag07_countAC+pre2000_countAC+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="negbin",data=data_all,robust=TRUE)
summary(m14)

#Table, relogit is not recognized, have to substitute real values
toLatex(
  relabel(
    mtable("Logit, robust SE"=m10,"Re-Logit, robust SE"=m10, "Mixed Logit"=m12,"Poisson, robust SE"=m13,"Negative Binomial, robust SE"=m14,coef.style="stat",
           summary.stats=c("AIC","BIC","Deviance","Log-likelihood","N")),
    splag07_dum="Spatial Lag",
    splag07_count="Spatial Lag",
    pre2000_count="pre-2000 Conflict",
    bdist1="Border Distance",
    capdist="Capital Distance",
    pop2005="Population",
    mnt="Pct Mountainous ",
    irri="Pct Irrigation",
    gcppc00="GDP pc",
    cell_dum_07="Cellphone Coverage",
    cmean_07="Mean Cell Coverage",
    gsub=TRUE),ddigits=5)


#IV
m15 <- ivreg(conf2008_dumAC~pre2000_countAC+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07|pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+reg,data=data_all)
summary(m15)

m16 <- ivreg(conf2008_dumAC~splag07_dumAC+pre2000_countAC+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07|splag07_dum+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+reg,data=data_all)
summary(m16)

#Table
toLatex(
  relabel(
    mtable("2SLS"=m15,"2SLS"=m16,coef.style="stat",
           summary.stats=c("AIC","BIC","Deviance","Log-likelihood","N")),
    splag07_dum="Spatial Lag",
    splag07_count="Spatial Lag",
    pre2000_count="pre-2000 Conflict",
    bdist1="Border Distance",
    capdist="Capital Distance",
    pop2005="Population",
    mnt="Pct Mountainous ",
    irri="Pct Irrigation",
    gcppc00="GDP pc",
    cell_dum_07="Cellphone Coverage",
    cmean_07="Mean Cell Coverage",
    gsub=TRUE),ddigits=5)




#####################################
#### CEM 
#####################################
library("cem")

m1 <- zelig(conf2008_dum~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+diamond+oil+cell_dum_07,model="logit",data=data_all,robust=TRUE)
summary(m1)


Le <- data_all[,c("conf2008_dum","pre2000_count","bdist1","capdist","pop2005","mnt","irri","gcppc00","cell_dum_07","conf2008_count")]

Le <- data.frame(na.omit(Le))

#calculate imbalance measure
vars <- c("pre2000_count","bdist1","capdist","pop2005","mnt","irri","gcppc00")

imbalance(group=Le$cell_dum_07,data=Le[vars])

mat <- cem(treatment="cell_dum_07",data=Le,drop=c("conf2008_dum","conf2008_count"))


est <- att(mat,conf2008_count~cell_dum_07+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00,data=Le)
est
plot(est,mat,Le,vars=c("capdist","pop2005","oil","gcppc00"))

est2 <- att(mat,conf2008_dum~cell_dum_07+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00,data=Le,model="linear-RE")
est2
plot(est2,mat,Le,vars=c("capdist","pop2005","oil","gcppc00"))

est3 <- att(mat,conf2008_dum~cell_dum_07,data=Le,model="logit")
est3

est4 <- att(mat,conf2008_dum~cell_dum_07+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00,data=Le,model="logit")
est4
plot(est3,mat,Le,vars=c("capdist","pop2005","oil","gcppc00"))




#####################################
#### Robustness SCAD
#####################################
#standard logit
m1 <- zelig(scad2008_dum~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="logit",data=data_all,robust=TRUE)
summary(m1)

#re-logit
m2 <- zelig(scad2008_dum~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="relogit",robust=TRUE,data=data_all)
summary(m2)

#mixed effects logit
m3 <- zelig(scad2008_dum~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07+tag(1|cow),model="logit.mixed",data=data_all)
summary(m3)

#mixed effects logit with country mean
m4 <- zelig(scad2008_dum~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07+cmean_07+tag(1|cow),model="logit.mixed",data=data_all)
summary(m4)

#OLS with fixed effects
m5 <- zelig(scad2008_dum~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07+factor(cow),model="ls",data=data_all,robust=TRUE)
summary(m5)

#Table, relogit is not recognized, have to substitute real values
toLatex(
  relabel(
    mtable("Logit, robust SE"=m1, "Re-Logit, robust SE"=m1,"Mixed Effects Logit"=m3,"Mixed Effects Logit"=m4,"Fixed Effects OLS, robust SE"=m5,coef.style="stat",
           summary.stats=c("AIC","BIC","Deviance","Log-likelihood","N")),
    pre2000_count="pre-2000 Conflict",
    bdist1="Border Distance",
    capdist="Capital Distance",
    pop2005="Population",
    mnt="Pct Mountainous ",
    irri="Pct Irrigation",
    gcppc00="GDP pc",
    cell_dum_07="Cellphone Coverage",
    cmean_07="Mean Cell Coverage",
    gsub=TRUE),ddigits=7)



m6 <- zelig(scad2008_count~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="poisson",data=data_all,robust=TRUE)
summary(m6)

m7 <- zelig(scad2008_count~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07+tag(1|cow),model="poisson.mixed",data=data_all,robust=TRUE)
summary(m7)

m8 <- zelig(scad2008_count~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07+cmean_07+tag(1|cow),model="poisson.mixed",data=data_all,robust=TRUE)
summary(m8)

m9 <- zelig(scad2008_count~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="negbin",data=new,robust=TRUE)
summary(m9)

#Table
toLatex(
  relabel(
    mtable("Poisson, robust SE"=m6, "Negative Binomial, robust SE"=m9,coef.style="stat",
           summary.stats=c("AIC","BIC","Deviance","Log-likelihood","N")),
    pre2000_count="pre-2000 Conflict",
    bdist1="Border Distance",
    capdist="Capital Distance",
    pop2005="Population",
    mnt="Pct Mountainous ",
    irri="Pct Irrigation",
    gcppc00="GDP pc",
    cell_dum_07="Cellphone Coverage",
    cmean_07="Mean Cell Coverage",
    gsub=TRUE),ddigits=5)


m10 <- zelig(scad2008_dum~splag07_dumScad+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="logit",data=data_all,robust=TRUE)
summary(m10)

m11 <- zelig(scad2008_dum~splag07_dumScad+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="relogit",data=data_all,robust=TRUE)
summary(m11)

m12 <- zelig(scad2008_dum~splag07_dumScad+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07+cmean_07+tag(1|cow),model="logit.mixed",data=data_all)
summary(m12)

m13 <- zelig(scad2008_count~splag07_countScad+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="poisson",data=data_all,robust=TRUE)
summary(m13)

m14 <- zelig(scad2008_count~splag07_countScad+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07,model="negbin",data=data_all,robust=TRUE)
summary(m14)

#Table, relogit is not recognized, have to substitute real values
toLatex(
  relabel(
    mtable("Logit, robust SE"=m10,"Re-Logit, robust SE"=m10, "Mixed Logit"=m12,"Poisson, robust SE"=m13,"Negative Binomial, robust SE"=m14,coef.style="stat",
           summary.stats=c("AIC","BIC","Deviance","Log-likelihood","N")),
    splag07_dumScad="Spatial Lag",
    splag07_countScad="Spatial Lag",
    pre2000_count="pre-2000 Conflict",
    bdist1="Border Distance",
    capdist="Capital Distance",
    pop2005="Population",
    mnt="Pct Mountainous ",
    irri="Pct Irrigation",
    gcppc00="GDP pc",
    cell_dum_07="Cellphone Coverage",
    cmean_07="Mean Cell Coverage",
    gsub=TRUE),ddigits=5)


#IV
m15 <- ivreg(scad2008_dum~pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07|pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+reg,data=data_all)
summary(m15)

m16 <- ivreg(scad2008_dum~splag07_dumScad+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+cell_dum_07|splag07_dumScad+pre2000_count+bdist1+capdist+pop2005+mnt+irri+gcppc00+reg,data=data_all)
summary(m16)

#Table
toLatex(
  relabel(
    mtable("2SLS"=m15,"2SLS"=m16,coef.style="stat",
           summary.stats=c("AIC","BIC","Deviance","Log-likelihood","N")),
    splag07_dum="Spatial Lag",
    splag07_count="Spatial Lag",
    pre2000_count="pre-2000 Conflict",
    bdist1="Border Distance",
    capdist="Capital Distance",
    pop2005="Population",
    mnt="Pct Mountainous ",
    irri="Pct Irrigation",
    gcppc00="GDP pc",
    cell_dum_07="Cellphone Coverage",
    cmean_07="Mean Cell Coverage",
    gsub=TRUE),ddigits=5)






